//
//  DeviceConfig.m
//  KEREN
//
//  Created by chenliang on 2023/8/22.
//

#import "DeviceConfig.h"
#import "NSDictionary+Expand.h"
@implementation DeviceConfig

+(NSDictionary *)replaceKeyFromPropertyName{
    return @{
             @"fourg" : @"4g",
             @"anti_theft_alarm" : @"anti-theft_alarm"
             };
}

+(NSArray *)cf_KeysWithValues{
    NSMutableArray *array = [[NSMutableArray alloc]init];
    unsigned int count ,i;
    objc_property_t *propertyArray =class_copyPropertyList([self class], &count);
    for(i =0; i < count; i++) {
     objc_property_t property = propertyArray[i];
     NSString *proKey = [NSString stringWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
     [array addObject:proKey];
    }
    return [array copy];
}

+(DeviceConfig *)parseFromJsonString:(NSString *)jsonString{
    NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *jsonDict = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    NSDictionary *dict = [jsonDict dictForDict:@"configs"];
    NSMutableArray *allArray = @[].mutableCopy;
    [allArray addObjectsFromArray:[dict arrayForDict:@"control"]];
    [allArray addObjectsFromArray:[dict arrayForDict:@"smart_config"]];
    [allArray addObjectsFromArray:[dict arrayForDict:@"common_functions"]];
    [allArray addObjectsFromArray:[dict arrayForDict:@"bike_info"]];
    [allArray addObjectsFromArray:[dict arrayForDict:@"run_info"]];
    [allArray addObjectsFromArray:[dict arrayForDict:@"mine"]];
    [allArray addObjectsFromArray:[dict arrayForDict:@"notice"]];
    DeviceConfig *deviceConfig = [DeviceConfig new];
    NSArray *property_array = [self cf_KeysWithValues];
    NSDictionary *replace_property_dict = [self replaceKeyFromPropertyName];
    NSArray *replace_value_array = [replace_property_dict allValues];
    NSArray *replace_key_array = [replace_property_dict allKeys];
    for(NSDictionary *dictInarray in allArray){
        NSString *property_key = dictInarray[@"key"];
        if([property_array containsObject:property_key]){
            int status = [dictInarray intForDict:@"status"];
            [deviceConfig setValue:@(status) forKeyPath:property_key];
        }else if([replace_value_array containsObject:property_key]){
            for(NSString *replace_key in replace_key_array){
                NSString *replace_value = replace_property_dict[replace_key];
                int status = [dictInarray intForDict:@"status"];
                if([property_key isEqualToString:replace_value]){
                    [deviceConfig setValue:@(status) forKeyPath:replace_key];
                }
            }
        }
    }
    return deviceConfig;
}

@end



@implementation DeviceConfigData



@end
